// import axios from "axios"
import APIClient from "../network/index"

const cartModule = {
    state: {
        navs: [],
        selectIndex: 0,
        navOne: [],
        navTwo: []
    },
    mutations: {
        getClassify(state, items) {
            //全部
            state.navs =[...items];
            //一级
            state.navs.forEach(element => {
                if (element.pid === 0) {
                    state.navOne.push(element);
                }
            });
            //二级默认
            var id = state.navOne[0].id;
            state.navs.forEach(element => {
                if (element.pid === id) {
                    state.navTwo.push(element);
                }
            });
        },
        //二级切换
        active(state, obj) {
            state.selectIndex = obj.index;
            state.navTwo = [];
            var id = obj.item.id;
            state.navs.forEach(element => {
                if (element.pid === id) {
                    state.navTwo.push(element);
                }
            });
        }
    },
    actions: {
        getClassify(context) {
            APIClient.getClassify()
                .then((res) => {
                    console.log(res.data.data)
                    context.commit("getClassify", res.data.data)
                }).catch((error) => {
                    console.log(error)
                })
        },
        active(context, index) {
            context.commit("active", index)
        }
    },
    modules: {

    }
}

export default cartModule
